<template>
  <div>
    <my-header :showBack="true" title="货币转换"></my-header>
    <div class="currency_conversion">
      <van-tabs v-model="active">
        <van-tab title="帮积分转换">
          <div class="currency_conversion_box">
            <van-cell-group style="margin-top: 10px;">
              <van-field
                :value="Math.floor(indexData.useMoney*100)/100"
                type="text"
                label="可用帮余额"
                disabled
              />
              <div class="van-cell van-hairline van-field">
                <div class="van-cell__title"><span>帮余额数量</span></div>
                <div class="van-cell__value">
                  <input type="number" v-model="moneyNum" placeholder="请输入帮余额数量" class="van-field__control">
                </div>
              </div>
              <div class="van-cell van-hairline van-field">
                <div class="van-cell__title"><span>帮积分数量</span></div>
                <div class="van-cell__value">
                  <input type="number" :value="Math.floor(Integral*100)/100" placeholder="转换帮积分数量"  class="van-field__control" readonly>
                </div>
              </div>
              <van-field
                v-model="payPass"
                type="password"
                label="支付密码"
                placeholder="请输入支付密码"
              />
            </van-cell-group>
            <div class="transfer_accounts_tip">新用户默认支付密码为：123456，您可以在账户设置中更改</div>
            <van-button type="primary" class="conversion_btn" :disabled="disabledBtn" @click="confirmTrade">确认兑换</van-button>
          </div>
          <div class="conversion_record">
            <p>兑换记录</p>
            <van-list v-model="loading" :finished="finished"  @load="loadMore" :offset="20">
              <table>
                <tr>
                  <th style="width: 150px;">时间</th>
                  <th>帮余额数量</th>
                  <th>帮积分数量</th>
                </tr>
                <tr v-for="item in recordList">
                  <td>{{item.createTime|formatDate}}</td>
                  <td>{{item.convertMoney}}</td>
                  <td>{{Math.floor(item.convertNum*100)/100}}</td>
                </tr>
              </table>
            </van-list>
          </div>
        </van-tab>
        <van-tab title="帮余额转换">
          <div class="currency_conversion_box">
            <van-cell-group style="margin-top: 10px;">
              <van-field
                :value="Math.floor(indexData.useCoin*100)/100"
                type="text"
                label="可用 CEC"
                disabled
              />
              <div class="van-cell van-hairline van-field">
                <div class="van-cell__title"><span>CEC 数量</span></div>
                <div class="van-cell__value">
                  <input type="number" v-model="cecNum" placeholder="请输入 CEC 数量" class="van-field__control">
                </div>
              </div>
              <div class="van-cell van-hairline van-field">
                <div class="van-cell__title"><span>帮余额数量</span></div>
                <div class="van-cell__value">
                  <input type="number" :value="Math.floor(money*100)/100" placeholder="转换帮余额数量"  class="van-field__control" readonly>
                </div>
              </div>
              <van-field
                v-model="payPass"
                type="password"
                label="支付密码"
                placeholder="请输入支付密码"
              />
            </van-cell-group>
            <div class="transfer_accounts_tip">新用户默认支付密码为：123456，您可以在账户设置中更改</div>
            <van-button type="primary" class="conversion_btn" :disabled="disabledCecBtn" @click="confirmTradeCec">确认兑换</van-button>
          </div>
          <div class="conversion_record">
            <p>兑换记录</p>
            <van-list v-model="loading1" :finished="finished1"  @load="loadMoreCec" :offset="20">
              <table>
                <tr>
                  <th style="width: 150px;">时间</th>
                  <th>CEC 数量</th>
                  <th>帮余额数量</th>
                </tr>
                <tr v-for="item in recordList1">
                  <td>{{item.createTime|formatDate}}</td>
                  <td>{{item.convertMoney}}</td>
                  <td>{{Math.floor(item.convertNum*100)/100}}</td>
                </tr>
              </table>
            </van-list>
          </div>
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>
<script>
import timeFormat from "../../store/timeFormat";
import header from "@/components/header";

import { Toast } from "vant";
export default {
  components: {
    "my-header": header
  },
  data() {
    return {
      active:0,
      indexData:{},
      payPass:'',

      moneyNum: "",
      Integral: "",
      disabledBtn: false,

      loading: false,
      finished: true,
      recordList: [],
      searchData: {
        pageNo: 1, //当前页
        pageSize: 30, //每页30条
        type: 1
      },

      cecNum: "",
      money: "",
      disabledCecBtn: false,

      loading1: false,
      finished1: true,
      recordList1: [],
      searchData1: {
        pageNo: 1, //当前页
        pageSize: 30, //每页30条
        type: 5
      }
    };
  },
  watch: {
    active(val) {
      if (val == 0) {
        this.cecNum = "";
        this.money = "";
        this.payPass = "";
        this.searchData1.pageNo = 1;
        this.init();
      } else {
        this.moneyNum = "";
        this.Integral = "";
        this.payPass = "";
        this.searchData.pageNo = 1;
        this.initCec();
      }
    },
    moneyNum(val) {
      if (!isNaN(val) && val > 0) {
        this.Integral = Math.floor(val * this.indexData.rateToIntegralMore * 10000) / 10000;
      } else {
        this.Integral = "";
      }
    },
    cecNum(val) {
      if (!isNaN(val) && val > 0) {
        this.money = Math.floor(val * this.indexData.moneyRate * 10000) / 10000;
      } else {
        this.money = "";
      }
    },
  },
  filters: {
    formatDate(time) {
      var date = new Date(time);
      return timeFormat.formatDate(date, "yyyy.MM.dd hh:mm:ss");
    }
  },
  mounted() {
    this.init();
    this.selectIndexData();
  },
  methods: {
    selectIndexData(){
      this.$ajax(this.$api.selectIndexData).then(res => {
        if (res.code == 0) {
          this.indexData = res.data;
        }
      });
    },
    init() {
      this.searchData.pageNo = 1;
      this.$ajax(this.$api.selectAccountConvert, this.searchData).then(res => {
        this.loading = false;
        if (res.code === 0) {
          if (res.data && res.data.list.length > 0) {
            this.recordList = res.data.list;
            this.finished = this.searchData.pageNo == res.data.pageCount;
            this.loading = false;
          }
        } else {
          this.recordList = [];
        }
      });
    },
    loadMore: function() {
      this.searchData.pageNo += 1;
      this.$ajax(this.$api.selectAccountConvert, this.searchData).then(res => {
        this.loading = false;
        if (res.code === 0) {
          if (res.data && res.data.list.length > 0) {
            this.recordList = this.recordList.concat(res.data.list);
            this.finished = this.searchData.pageNo == res.data.pageCount;
            this.loading = false;
          }
        }
      });
    },
    confirmTrade() {
      if (isNaN(this.moneyNum)) {
        Toast({ message: this.$t('lang.EnterTrueNum'), position: "bottom" });
        return;
      } else if (this.moneyNum <= 0) {
        Toast({ message: "请输入大于0的数量", position: "bottom" });
        return;
      }
      //  else if(this.moneyNum%10!=0){
      //   Toast({ message: "转换数量必须是10的倍数", position: "bottom" });
      //   return;
      // }
      if (this.payPass == "") {
        Toast({ message: "请输入支付密码", position: "bottom" });
        return;
      }
      this.disabledBtn = true;
      this.$ajax(this.$api.doMoneryToIntegral, {
        moneyNum: this.moneyNum,
        payPass:this.payPass
      }).then(res => {
        if (res.code == 0) {
          Toast({ message: "转换成功", position: "bottom" });
          this.init();
          this.selectIndexData();
          this.moneyNum = "";
          this.payPass = ""
        }
        this.disabledBtn = false;
      });
    },

    initCec() {
      this.searchData1.pageNo = 1;
      this.$ajax(this.$api.selectAccountConvert, this.searchData1).then(res => {
        this.loading = false;
        if (res.code === 0) {
          if (res.data && res.data.list.length > 0) {
            this.recordList1 = res.data.list;
            this.finished1 = this.searchData1.pageNo == res.data.pageCount;
            this.loading1 = false;
          }
        } else {
          this.recordList1 = [];
        }
      });
    },
    loadMoreCec() {
      this.searchData1.pageNo += 1;
      this.$ajax(this.$api.selectAccountConvert, this.searchData1).then(res => {
        this.loading1 = false;
        if (res.code === 0) {
          if (res.data && res.data.list.length > 0) {
            this.recordList1 = this.recordList.concat(res.data.list);
            this.finished1 = this.searchData1.pageNo == res.data.pageCount;
            this.loading1 = false;
          }
        }
      });
    },
    confirmTradeCec() {
      if (isNaN(this.cecNum)) {
        Toast({ message: this.$t('lang.EnterTrueNum'), position: "bottom" });
        return;
      } else if (this.cecNum <= 0) {
        Toast({ message: "请输入大于0的数量", position: "bottom" });
        return;
      } 
      // else if(this.cecNum%50!=0){
      //   Toast({ message: "转换数量必须是50的倍数", position: "bottom" });
      //   return;
      // }
      if (this.payPass == "") {
        Toast({ message: "请输入支付密码", position: "bottom" });
        return;
      }
      this.disabledCecBtn = true;
      this.$ajax(this.$api.doCoinToMoney, {
        moneyNum: this.cecNum,
        payPass:this.payPass
      }).then(res => {
        if (res.code == 0) {
          Toast({ message: "转换成功", position: "bottom" });
          this.initCec();
          this.selectIndexData();
          this.cecNum = "";
          this.payPass = "";
        }
        this.disabledCecBtn = false;
      });
    }
  }
};
</script>

<style scoped>
.currency_conversion {
  background: #fff;
  font-size: 14px;
}
.currency_conversion_box {
  background: #fff;
}
.transfer_accounts_tip {
  font-size: 12px;
  color: #d20000;
  margin: 5px 15px;
}
.conversion_box {
  display: flex;
  padding: 15px 10px 0;
  align-items: center;
}
.conversion_box input {
  padding: 10px;
  margin: 0;
  width: 100%;
  font-size: 14px;
  border: none;
  background: #f4f4f4;
  border-radius: 0;
  height: auto;
  box-sizing: border-box;
  color: #888;
  text-align: center;
}
.conversion_box div {
  flex: 1;
}
.conversion_box div:nth-child(2) {
  max-width: 40px;
  text-align: center;
  font-size: 20px;
  color: #25a3fe;
}

.conversion_btn {
  width: 40%;
  margin: 10px 30%;
  text-align: center;
  background: #25a3fe;
  border: 1px solid #25a3fe;
  color: #fff;
  line-height: 38px;
  height: 38px;
  border-radius: 5px;
  display: inline-block;
}
.conversion_record {
  margin-top: 10px;
}
.conversion_record p {
  padding: 0 10px;
  line-height: 40px;
  background: #fff;
}
.conversion_record table {
  width: 100%;
  word-wrap: break-word;
  word-break: break-all;
  text-align: center;
  border-collapse: collapse;
  padding: 2px;
  border: 1px solid #f1f1f1;
  color: #333;
}
.conversion_record tr {
  border: 1px solid #f1f1f1;
}
.conversion_record th {
  border: 1px solid #f1f1f1;
  padding: 10px 0;
  background: #fff;
  color: #666;
}
.conversion_record td {
  border: 1px solid #f1f1f1;
  padding: 10px 0;
  background: #fff;
  color: #888;
}
</style>
